#include "ZenLine.h"

CZenLine::CZenLine()
{
}

CZenLine::CZenLine(float x1, float y1, float z1, D3DCOLOR StartColor,
				   float x2, float y2, float z2, D3DCOLOR EndColor)
{
	m_StartPoint.m_Position.x = x1;
	m_StartPoint.m_Position.y = y1;
	m_StartPoint.m_Position.z = z1;

	m_StartPoint.m_DiffuseColor = StartColor;

	m_EndPoint.m_Position.x = x2;
	m_EndPoint.m_Position.y = y2;
	m_EndPoint.m_Position.z = z2;

	m_EndPoint.m_DiffuseColor = EndColor;
}

CZenLine::~CZenLine()
{
}

void CZenLine::SetProps(float x1, float y1, float z1, D3DCOLOR StartColor,
						float x2, float y2, float z2, D3DCOLOR EndColor)
{
	m_StartPoint.m_Position.x = x1;
	m_StartPoint.m_Position.y = y1;
	m_StartPoint.m_Position.z = z1;

	m_StartPoint.m_DiffuseColor = StartColor;

	m_EndPoint.m_Position.x = x2;
	m_EndPoint.m_Position.y = y2;
	m_EndPoint.m_Position.z = z2;

	m_EndPoint.m_DiffuseColor = EndColor;
}

void CZenLine::GetProps(float &x1, float &y1, float &z1, D3DCOLOR &StartColor,
						float &x2, float &y2, float &z2, D3DCOLOR &EndColor)
{
	x1 = m_StartPoint.m_Position.x;
	y1 = m_StartPoint.m_Position.y;
	z1 = m_StartPoint.m_Position.z;

	StartColor = m_StartPoint.m_DiffuseColor;

	x2 = m_EndPoint.m_Position.x;
	y2 = m_EndPoint.m_Position.y;
	z2 = m_EndPoint.m_Position.z;

	EndColor = m_EndPoint.m_DiffuseColor;
}

HRESULT CZenLine::Render()
{
	HRESULT res  = 0;
	BYTE * pData = 0;

	LPDIRECT3DVERTEXBUFFER8 pVB = 0;

	res = zen.m_pDevice->CreateVertexBuffer(sizeof(CZenVertex)*2, D3DUSAGE_WRITEONLY, 
											ZENVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB);
	if (FAILED(res))
	{
		return E_FAIL;
	}

	res = pVB->Lock(0, 0, &pData, 0);
	if (FAILED(res))
	{
		pVB->Release();
		return E_FAIL;
	}
	
	CopyMemory(pData, (void*)&m_StartPoint, sizeof(CZenVertex));
	CopyMemory(pData + sizeof(CZenVertex), (void*)&m_EndPoint, sizeof(CZenVertex));

	pVB->Unlock();

	zen.m_pDevice->SetStreamSource(0, pVB, sizeof(CZenVertex));

	zen.m_pDevice->DrawPrimitive(D3DPT_LINELIST, 0 ,1);

	pVB->Release();

	return S_OK;
}
